package com.wanke.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.wanke.bean.CheckinDormitoryRecordBean;
import com.wanke.bean.QueryCheckDormitoryCondition;
import com.wanke.entity.Dormitory;
import com.wanke.entity.Teacher;
import com.wanke.service.DormitoryService;

@Controller
@RequestMapping(value = "/dormitory")
public class DormitorySystemContoller {

	@Resource(name = "dormitoryService")
	DormitoryService dormitoryService;

	/*
	 * 耀锋
	 */
	Gson gson = new Gson();

	@RequestMapping(value = "/add_dormitory", method = RequestMethod.POST)
	public @ResponseBody String addDormitory(Dormitory dormitory, String counselor_id) {
		Map<String, Object> map = dormitoryService.addDormitory(dormitory, counselor_id);
		return gson.toJson(map);
		// Map<String, Object> map=new HashMap<>();
		// System.out.println(dormitory.toString());
		// map.put("result", "controller result");
		// return gson.toJson(map);
	}

	@RequestMapping(value = "/update_dormitory", method = RequestMethod.POST)
	public @ResponseBody String updateDormitory(Dormitory dormitory) {
		Map<String, Object> map = dormitoryService.updateDormitory(dormitory);
		Gson gson = new Gson();
		return gson.toJson(map);
	}

	@RequestMapping(value = "/get_checking_dormitory_info", method = RequestMethod.POST)
	public @ResponseBody String getMembers(HttpSession session) {
		// int student_id =
		// Integer.parseInt(session.getAttribute("student_id").toString());
		int student_id = 6;
		return gson.toJson(dormitoryService.getCheckingDormitoryInfo(student_id));
	}

	@RequestMapping(value = "/checkin_dormitory", method = RequestMethod.POST)
	public @ResponseBody String CheckinDormitory(String result) {
		Map<String, Object> resultMap = new HashMap<>();

		result = result.substring(0, result.length() - 2);
		String[] resultSingles = result.split("&&");
		for (String single : resultSingles) {
			String[] singles = single.split(",");
			Map<String, Object> map = dormitoryService.addDormitoryCheckIn(Integer.parseInt(singles[0]), singles[1]);
			resultMap.put(singles[0], gson.toJson(map));
			map.clear();
		}

		return gson.toJson(resultMap);
	}

	@RequestMapping(value = "/get_daily_checkin_data", method = RequestMethod.POST)
	public @ResponseBody String getDailyCheckinData(Teacher teacher, String query_type) {
		List<CheckinDormitoryRecordBean> result = dormitoryService.getCheckinDataAsTeacher(teacher);
		Map<String, Object> map = new HashMap<>();
		map.put("banji_name", result.get(0).getBanji_name());
		map.put("identity_type", teacher.getType());
		map.put("query_type", query_type);
		map.put("records", result);
		System.out.println(getClass().toString() + "-->" + gson.toJson(map));
		return gson.toJson(map);
	}

	@RequestMapping(value = "/screen_data", method = RequestMethod.POST)
	public @ResponseBody String screenData(QueryCheckDormitoryCondition condition, HttpSession session) {

		int teacher_id = 1; // 模拟从session 获取
		condition.setTeacher_id(teacher_id);
		System.out.println(condition.toString());
		List<CheckinDormitoryRecordBean> cdrbs = dormitoryService.screenCheckinData(condition);
		Map<String, Object> map = new HashMap<>();
		map.put("banji_name", cdrbs.get(0).getBanji_name());
		map.put("identity_type", condition.getIdentity_type());
		map.put("query_type", condition.getQuery_type());
		map.put("records", cdrbs);
		return gson.toJson(map);

	}
}
